﻿using System.Globalization;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

//此文件只是些备用代码。
namespace SHomeWorkshop.LunarPresentation.Adorners
{
    #region 文本块任务列表装饰器，与左缩进冲突。废弃。
    ///// <summary>
    ///// 文本块任务列表装饰器。
    ///// </summary>
    //public class TaskItemAdorner : Adorner
    //{
    //    /// <summary>
    //    /// 文本块的任务列表装饰器。
    //    /// </summary>
    //    /// <param name="adornedElement">应传入主窗口的“MainTabControl”主子元素。</param>
    //    public TaskItemAdorner(Expressions.BaseText adornedElement)
    //        : base(adornedElement)
    //    {
    //        //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
    //        this.SnapsToDevicePixels = true;
    //        this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

    //        this.masterBt = adornedElement;

    //        this.Cursor = Cursors.Arrow;

    //        this.MouseDown += new MouseButtonEventHandler(TaskItemAdorner_MouseDown);
    //    }

    //    void TaskItemAdorner_MouseDown(object sender, MouseButtonEventArgs e)
    //    {
    //        if (masterBt == null) return;

    //        Expressions.BaseText.TaskType newTaskType = Expressions.BaseText.TaskType.NotTask;

    //        switch (masterBt.TaskItemType)
    //        {
    //            case Expressions.BaseText.TaskType.NotTask: return;
    //            case Expressions.BaseText.TaskType.Aborted:
    //                {
    //                    newTaskType = Expressions.BaseText.TaskType.NotStarted; break;
    //                }
    //            case Expressions.BaseText.TaskType.Finished:
    //                {
    //                    newTaskType = Expressions.BaseText.TaskType.Aborted; break;
    //                }
    //            case Expressions.BaseText.TaskType.NotStarted:
    //                {
    //                    newTaskType = Expressions.BaseText.TaskType.Running; break;
    //                }
    //            case Expressions.BaseText.TaskType.Paused:
    //                {
    //                    newTaskType = Expressions.BaseText.TaskType.Finished; break;
    //                }
    //            case Expressions.BaseText.TaskType.Running:
    //                {
    //                    newTaskType = Expressions.BaseText.TaskType.Paused; break;
    //                }
    //        }

    //        if (masterBt.TaskItemType != newTaskType)
    //        {
    //            ModifingItem mi = new ModifingItem();

    //            Action act = new Action(masterBt.ExID, -1, masterBt.GetType().Name,
    //                "TaskItemType", masterBt.TaskItemType.ToString(), newTaskType.ToString());

    //            masterBt.TaskItemType = newTaskType;

    //            mi.Add(act);

    //            masterBt.Editor.Do(mi);

    //            masterBt.Editor.IsModified = true;
    //        }
    //    }
    //    private Expressions.BaseText masterBt;

    //    /// <summary>
    //    /// 使用本任务列表框的文本块。
    //    /// </summary>
    //    public Expressions.BaseText MasterBt
    //    {
    //        get { return masterBt; }
    //        set { masterBt = value; }
    //    }

    //    static TaskItemAdorner()
    //    {
    //        transPen = new Pen(Brushes.Transparent, 1);
    //    }

    //    private static Pen transPen;

    //    protected override void OnRender(DrawingContext drawingContext)
    //    {
    //        if (MasterBt == null || masterBt.TaskItemType == Expressions.BaseText.TaskType.NotTask) return;

    //        Rect sourceRect = new Rect(this.AdornedElement.RenderSize);
    //        Rect rect = new Rect(new Point(sourceRect.Left + 2, sourceRect.Height / 2 - masterBt.TextBox.FontSize / 2),
    //            new Size(masterBt.TextBox.FontSize, masterBt.TextBox.FontSize));


    //        Brush renderBrush = masterBt.ExForeground;
    //        Pen renderPen = new Pen(renderBrush, 1) { StartLineCap = PenLineCap.Square };

    //        drawingContext.DrawRectangle(Brushes.Transparent, renderPen, rect);

    //        switch (masterBt.TaskItemType)
    //        {
    //            case Expressions.BaseText.TaskType.Aborted:
    //                {
    //                    drawingContext.DrawLine(renderPen,
    //                        new Point(rect.Left + 2, rect.Top + 2),
    //                        new Point(rect.Right - 3, rect.Bottom - 3));
    //                    drawingContext.DrawLine(renderPen,
    //                        new Point(rect.Right - 3, rect.Top + 2),
    //                        new Point(rect.Left + 2, rect.Bottom - 3));
    //                    break;
    //                }
    //            case Expressions.BaseText.TaskType.Finished:
    //                {
    //                    drawingContext.DrawRectangle(renderBrush, renderPen,
    //                        new Rect(new Point(rect.Left + 2, rect.Top + 2),
    //                            new Size(rect.Width - 4, rect.Height - 4)));
    //                    break;
    //                }
    //            //case Expressions.BaseText.TaskType.NotStarted://已绘制空框。
    //            //case Expressions.BaseText.TaskType.NotTask://已返回。
    //            case Expressions.BaseText.TaskType.Paused:
    //                {
    //                    drawingContext.DrawRectangle(renderBrush, renderPen,
    //                        new Rect(new Point(rect.Left + 3, rect.Top + 2),
    //                            new Size((rect.Width - 4) / 2 - 2, rect.Height - 4)));
    //                    drawingContext.DrawRectangle(renderBrush, renderPen,
    //                        new Rect(new Point(rect.Left + 2 + (rect.Width - 4) / 2 + 1, rect.Top + 2),
    //                            new Size((rect.Width - 4) / 2 - 2, rect.Height - 4)));
    //                    break;
    //                }
    //            case Expressions.BaseText.TaskType.Running:
    //                {
    //                    Point ptLT = new Point(rect.Left + 2, rect.Top + 2);
    //                    Point ptRT = new Point(rect.Right - 3, rect.Top + 2);
    //                    Point ptLB = new Point(rect.Left + 2, rect.Bottom - 3);

    //                    PathGeometry pathGeometry = new PathGeometry();
    //                    PathFigure pf = new PathFigure() { StartPoint = ptLT };
    //                    PolyLineSegment pls = new PolyLineSegment(
    //                        new List<Point>() { ptRT, ptLB, ptLT }, true);
    //                    pf.Segments.Add(pls);
    //                    pathGeometry.Figures.Add(pf);

    //                    drawingContext.DrawGeometry(renderBrush, renderPen, pathGeometry);

    //                    break;
    //                }
    //        }
    //    }
    //}

    #endregion

    #region 命令模式使用的装饰器。
    /// <summary>
    /// 主窗口的MainTabControl使用的装饰器。用来代替状态条文本区。
    /// </summary>
    public class CmdTextAdorner : Adorner
    {
        /// <summary>
        /// 主窗口的MainTabControl使用的装饰器。用来在状态条上侧显示当前输入的命令。
        /// </summary>
        /// <param name="adornedElement">应传入公式的“MainBorder”主子元素。</param>
        public CmdTextAdorner(UIElement adornedElement)
            : base(adornedElement)
        {
            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

            this.ToolTip = "回车执行命令，Esc键取消。";
        }

        private string cmdText;

        public string CmdText
        {
            get { return cmdText; }
            set
            {
                cmdText = value;
                this.Visibility = Visibility.Visible;
                InvalidateVisual();
            }
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            Rect rect = new Rect(this.AdornedElement.RenderSize);

            Brush renderBrush = Brushes.Black;

            if (cmdText != null && cmdText.Length > 0)
            {
                //背景矩形块。
                drawingContext.DrawRectangle(Brushes.WhiteSmoke,
                    new Pen(Brushes.LightBlue, 1),
                    new Rect(rect.Left + 39, rect.Bottom - 28, rect.Width - 77, 20));

                drawingContext.DrawText(
                    new FormattedText(cmdText,
                        CultureInfo.GetCultureInfo("en-us"),
                        FlowDirection.LeftToRight,
                        new Typeface("Arial"),
                        14, renderBrush),
                        new Point(rect.Left + 44, rect.Bottom - 26)
                    );
            }
        }
    }
    #endregion

    #region 备用类
    /// <summary>
    /// 主窗口的MainTabControl使用的装饰器。用来代替状态条文本区。
    /// </summary>
    //public class StatusTextAdorner : Adorner
    //{
    //    /// <summary>
    //    /// 主窗口的MainTabControl使用的装饰器。用来代替状态条文本区。
    //    /// </summary>
    //    /// <param name="adornedElement">应传入公式的“MainBorder”主子元素。</param>
    //    public StatusTextAdorner(UIElement adornedElement)
    //        : base(adornedElement)
    //    {
    //        //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
    //        this.SnapsToDevicePixels = true;
    //        this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

    //        this.MouseEnter += new MouseEventHandler(StatusTextAdorner_MouseEnter);
    //    }

    //    void StatusTextAdorner_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    //    {
    //        this.Visibility = Visibility.Hidden;
    //    }

    //    private string statusText;

    //    public string StatusText
    //    {
    //        get { return statusText; }
    //        set
    //        {
    //            statusText = value;
    //            this.Visibility = Visibility.Visible;
    //            InvalidateVisual();
    //        }
    //    }

    //    protected override void OnRender(DrawingContext drawingContext)
    //    {
    //        Rect rect = new Rect(this.AdornedElement.RenderSize);

    //        Brush renderBrush;
    //        if (Globals.selectedEditor != null)
    //        {
    //            renderBrush = Globals.selectedEditor.ExSelectionAdornerBrush;
    //        }
    //        else
    //        {
    //            renderBrush = Brushes.Black;
    //        }

    //        if (statusText != null && statusText.Length > 0)
    //        {
    //            drawingContext.DrawText(
    //                new FormattedText(statusText,
    //                    CultureInfo.GetCultureInfo("zh-cn"),
    //                    FlowDirection.LeftToRight,
    //                    new Typeface("Simsun"),
    //                    14, renderBrush),
    //                    new Point(rect.Left + 4, rect.Bottom - 24)
    //                );
    //        }
    //    }
    //}
    #endregion
}
